package com.cash.app.manager

import com.cash.app.constant.AppConstant.K_APP_CHECK
import com.cash.app.constant.AppConstant.K_APP_LOGIN
import com.cash.app.constant.AppConstant.K_WX_CODE
import com.cash.app.constant.AppConstant.LOGIN_IS_CHECK
import com.cash.app.ui.login.model.LoginResponse
import com.cash.app.utils.SpUtils

/**
 * Login state management class
 */
object LoginManager {

    private var userInfo: LoginResponse? = null
    private var token: String = ""

    /**
     * Update login state and token
     */
    fun updateLoginState(response: LoginResponse) {
        SpUtils.put(K_WX_CODE, response.token)
        setToken(response.token)
        SpUtils.put(K_APP_LOGIN, true)
        SpUtils.put(K_APP_CHECK, (response.is_check == LOGIN_IS_CHECK))
        userInfo = response
    }

    fun getUserInfo(): LoginResponse? {
        return userInfo
    }

    fun getToken(): String {
        return token
    }

    fun setToken(t: String) {
        token = t
    }

    /**
     * Clear login state
     */
    fun clearLoginState() {
        SpUtils.remove(K_WX_CODE)
        SpUtils.remove(K_APP_LOGIN)
        userInfo = null
    }

    /**
     * Check if user is login
     */
    fun isLogin(): Boolean {
        return SpUtils.get(K_APP_LOGIN, false)
    }

    fun isCheck(): Boolean {
        return SpUtils.get(K_APP_CHECK, false)
    }

    fun isNew(): Boolean {
        return if (userInfo === null) return false else userInfo!!.is_register == 1
    }

    fun clear() {
        userInfo = null
        token = ""
    }
} 